﻿
using Service.src.NW.Enum;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Service.src.NW.Model.Folw
{

    /// <summary>
    /// 节点执行之后的结果值
    /// </summary>
    public class NodeOutParam : NodeDynamicInputOutParamBaseEntity
    {

        public object Prototype { get; set; }

        //public NodeOutParamDatabase Database { get; set; }

        //public NodeOutParamLogic Logic { get; set; }

        //public NodeOutParamMes Mes { get; set; }

        //public NodeOutParamPlc Plc { get; set; }

        public List<NodeDynamicInputOutParamBaseEntity> OutParams { get; set; } = new List<NodeDynamicInputOutParamBaseEntity>();

        // public NodePLCOutParam NodePLC { get; set; }

        /// <summary>
        /// 默认一个输出参数
        /// </summary>
        public NodeOutParam()
        {
            AddOutParam(NodeOutParamNameEnum.NODE_STATUS.ToString(), "int");
        }

        /// <summary>
        /// 添加输出参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public void AddOutParam<T>(T t)
        {
            Type type = t.GetType();
            string ClassName = type.Name;

            PropertyInfo[] pi = type.GetProperties();

            foreach (PropertyInfo item in pi)
            {
                AddOutParam($"{ClassName}.{item.Name}", item.GetType().ToString());
            }
        }

        public void AddOutParam(List<NodeDataSource> OutParams)
        {
            if (OutParams == null)
            {
                return;
            }
            foreach (var item in OutParams)
            {
                AddOutParam(item.ParamName, item.ValueType);
            }
        }

        /// <summary>
        /// 添加输出参数
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="ValueType"></param>
        public void AddOutParam(string Param, string ValueType)
        {
            int Index = OutParams.FindIndex(o => o.ParamName == Param);
            if (Index == -1)
            {
                NodeDynamicInputOutParamBaseEntity OutParam = new NodeDynamicInputOutParamBaseEntity();
                OutParam.ParamName = Param;
                OutParam.ValueType = ValueType;
                OutParams.Add(OutParam);
            }
        }

        /// <summary>
        /// 设置输出属性的值
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="Value"></param>

        public void SetOutParamValue(string Param, dynamic Value)
        {
            foreach (NodeDynamicInputOutParamBaseEntity item in OutParams)
            {
                if (item.ParamName == Param)
                {
                    item.Value = Value;
                    break;
                }
            }
        }

        public void SetOutParamValue<T>(T t)
        {
            Type type = t.GetType();
            string ClassName = type.Name;
            PropertyInfo[] pi = type.GetProperties();

            foreach (PropertyInfo item in pi)
            {
                SetOutParamValue($"{ClassName}.{item.Name}", item.GetValue(t));
            }
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public dynamic GetOutParamValue(string Param)
        {
            dynamic Value = null;
            foreach (NodeDynamicInputOutParamBaseEntity Out in OutParams)
            {
                if (Out.ParamName == Param)
                {
                    Value = Out.Value;
                    break;
                }
            }
            return Value;
        }
    }
}
